package cn.cuilan.httpfileserver;

import java.io.InputStream;
import java.util.Properties;

/**
 * HTTP文件服务器启动类
 *
 * @author zhang.yan
 * @date 2020/3/21
 */
public class App {

    /**
     * 默认服务器IP
     */
    private static final String DEFAULT_SERVER_HOST = "127.0.0.1";

    /**
     * 默认服务器端口80
     */
    private static final int DEFAULT_SERVER_PORT = 80;

    /**
     * Windows默认服务器代理路径：C:/
     */
    private static final String DEFAULT_WINDOWS_PROXY_DIR = "C:/";

    /**
     * UNIX默认服务器代理路径：/
     */
    private static final String DEFAULT_UNIX_PROXY_DIR = "/";

    public static void main(String[] args) throws Exception {
        Properties properties = new Properties();
        InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("setting.properties");
        properties.load(is);
        String host = properties.getProperty("server.host");
        String port = properties.getProperty("server.port");
        String dir = properties.getProperty("proxy.dir");
        String hidden = properties.getProperty("show.hidden.file");
        String invaild = properties.getProperty("show.invaild.symbol");

        // 服务器IP或域名
        String serverHost = host == null || "".equals(host) ? DEFAULT_SERVER_HOST : host;
        // 端口
        int serverPort = port == null || "".equals(port) ? DEFAULT_SERVER_PORT : Integer.parseInt(port);
        // 代理目录
        String proxyDir;
        if (dir == null || "".equals(dir)) {
            // 如果系统环境为Windows
            if (System.getProperty("os.name").toLowerCase().startsWith("win")) {
                proxyDir = DEFAULT_WINDOWS_PROXY_DIR;
            } else {
                proxyDir = DEFAULT_UNIX_PROXY_DIR;
            }
        } else {
            proxyDir = dir;
        }
        // 是否显示隐藏文件
        boolean showHiddenFile = hidden != null && !"".equals(hidden);
        // 是否显示非法字符
        boolean showInvaildSymbol = invaild != null && !"".equals(invaild);
        System.out.println("server host: " + serverHost);
        System.out.println("server port: " + serverPort);
        System.out.println("proxy dir: " + proxyDir);
        System.out.println("show hidden file: " + showHiddenFile);
        System.out.println("show invaild symbol: " + showInvaildSymbol);
        new HttpFileServer().run(serverHost, serverPort, proxyDir, showHiddenFile, showInvaildSymbol);
    }
}
